package com.cssl.controller;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.nacos.shaded.com.google.gson.JsonArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.api.StaffApi;
import com.cssl.pojo.SysDept;
import com.cssl.serice.SysDeptSerice;
import com.cssl.serice.impl.SysDeptimpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//部门 //8010 端口号
@RestController
public class SysDeptController {

    @Resource
    private SysDeptSerice serice;

//    @Resource
//    private StaffApi api;

    @GetMapping("/querydept")
    public List<SysDept> liqy(){
        return serice.dgcy();
    }



    //查询部门的id每个大部门
    //直接查询0就是大部门的全部然后根据查出来的大部门的id去差子部门
    @RequestMapping("/dgquery")
        public  List<SysDept> dgcy1(){
      return serice.dgcyss(0);
    }

    //查询每个部门下的各个小部门传部门
    @RequestMapping("/ll")
    public  List<SysDept> ll(int id){
        return serice.ll(id);
    }

    @GetMapping("/cx")
    public IPage<SysDept> cx(Integer pageIndex,Integer pageSize){
        if (pageIndex == null || pageIndex == 0){
            pageIndex=1;
        }
        if (pageSize == null || pageSize ==0){
            pageSize=5;
        }
        return serice.select(new Page<SysDept>(pageIndex,pageSize));
    }

    @GetMapping("/add")
    public boolean add(String name,String phone,String email,int deptid,String date,String remark){
        SysDept dept=new SysDept();
        dept.setDname(name);
        dept.setCreate_time(date);
        dept.setEmail(email);
        dept.setParent_id(deptid);
        dept.setPhone(phone);
        dept.setRemark(remark);
        if (serice.save(dept)){
            return true;
        }
        return  false;
    }
    @GetMapping("/update")
    public boolean update(String name,String phone,String email,int deptid,String date,String remark,int status,int id){
        SysDept dept=new SysDept();
        dept.setDname(name);
        dept.setCreate_time(date);
        dept.setEmail(email);
        dept.setParent_id(deptid);
        dept.setId(id);
        dept.setStatus(status);
        dept.setPhone(phone);
        dept.setRemark(remark);
        if (serice.updateById(dept)){
            return true;
        }
        return  false;
    }

    @GetMapping("/department")
    public List<Map<String, Object>> dept(){
        List<SysDept> parentList = serice.list(new QueryWrapper<SysDept>().eq("parent_id", 0));
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (SysDept parentDept : parentList) {
            List<SysDept> list = serice.list(new QueryWrapper<SysDept>().eq("parent_id", parentDept.getId()));
            List<Integer> ids = list.stream().map(SysDept::getId).collect(Collectors.toList());
            System.out.println(ids);
            long num = serice.selectDept(ids);
            Map<String, Object> map = new HashMap<>();
            map.put("value", num);
            map.put("name", parentDept.getDname());
            mapList.add(map);
        }
        return mapList;
    }

    @GetMapping("/delete")
    public Boolean delete(int id){
        SysDept sysDept=new SysDept();
        sysDept.setId(id);
        sysDept.setIs_deleted(1);
        if (serice.updateById(sysDept)){
            return true;
        }
        return false;
    }
}
